/*
 *	contact-view view
 *  -----------------------------------------------------------------------------------------------------------
 */

site.view('contact-view', {
	
	onLoad: function(settings) {
		this.startAll(settings);
	},
	
	onUnload: function() {
		this.stopAll();
	}
})

/*
 *	contact main view manager (internal navigation) module
 *  -----------------------------------------------------------------------------------------------------------
 */ 

.module('main_view_manager', function(context) {
	var query = context.query,
		request	= context.request,
		require = context.require,
		tipsy = require('tipsy'),
		livequery = require('livequery'),

		processing = false,
		contentContainer,
		internalActions,
		rLastUrlPart = /[^\/]*$/,
		currentModuleView = '',

		onMenuClick = function(e) {
			var anchor = $(this),
	       		action = anchor.attr('href'),
	       		old_content_height = contentContainer.innerHeight(),
	       	 	old_content = contentContainer.children('div'),
	       	 	urlName = anchor.find('.mini_name').text(),

	       	 	actionName = action.substring(action.indexOf('contact'))
								   .replace('contact/', '')
								   .replace('contact', '')
								   .split('/');
       
			if (!$(this).children('div').hasClass('active') && !processing) {
				processing = true;

				$('#content > aside .list_main_container').removeClass('active');
				$(this).children('div').addClass('active');

				request({
				    url: action,
				    dataType: 'json',
				    cache: false,
				    success: function(json) {             
				        contentContainer.css({'height':old_content_height + 'px',
				                               'overflow':'hidden'});

				        var new_content = old_content.after(json.content).next('div'),              
				       		new_content_height = new_content.innerHeight(),
				        	animation_time = 700;
				        
				        old_content.children('.header_menu')
				                   .css('position','relative')
				                   .next('div')
				                   .css('padding-top','0px');
				      
				       	// Load new view module (and inform about change) 
				        context.trigger(currentModuleView[0] + 'ViewUnloaded', currentModuleView.slice(1));
				        context.trigger(actionName[0] + 'ViewLoaded', actionName.slice(1));
				        currentModuleView = actionName;
				        
				        old_content.animate({'margin-top': -old_content_height +'px'},animation_time,function() {
    
				            $(this).remove();
				        });

				        contentContainer.animate({'height': new_content_height + 'px'},animation_time,function() {
				            $('#content > section').css('height','auto')
				                                    .children('div')
				                                    .children('.header_menu')
				                                    .css('position','absolute')
				                                    .next('div')
				                                    .css('padding-top','47px');

				            processing = false;
				            window.history.pushState(urlName, urlName, anchor.attr('href'));
				        });        
				    }
				});      
			}
       		e.preventDefault();
       		return false;
    
		};

	return {

		init: function(settings) {
			contentContainer = query('#content > section');
			internalActions = query('.internal_action');

			internalActions.on('click', onMenuClick);

			// init default view (from url)
			
			var href = document.location.href;
			currentModuleView = href.substring(href.indexOf('contact'))
									.replace('contact/', '')
									.replace('contact', '')
									.split('/');

			currentModuleView = currentModuleView[0] !== '' ? currentModuleView : ['pending_requests'] ;
			context.trigger(currentModuleView[0] + 'ViewLoaded', currentModuleView.slice(1));

			// tipsy forms		
			livequery.init('input, textarea', function() {
				tipsy.init(this, {
					gravity: query(window).width() < 1260 ? 's' : 'w',
					trigger: 'focus'
				})
			});

			// tipsy actions
			livequery.init('.tooltip', function(){
        		this.tipsy({gravity:'se'});
    		});		
		},

		destroy: function() {
			internalActions.off(onMenuClick);
		}

	}

})

/*
 *	all contact view module
 *  -----------------------------------------------------------------------------------------------------------
 */ 

.module('all_view', function(context) {
	var query = $ = context.query,
		request	= context.request,
		require = context.require,
		notify = require('notify'),
		dialog = require('actionDialog');
	
	return {
		events: {

			allViewLoaded: function(subView) {
				console.log('all loaded');

				dialog.create('.delete', function(anchor) {
					request({
						url: anchor.attr('href'),
						type: 'post',
						dataType: 'json',
						success: function(data) {

							if(data.success == true){
								notify.create('create', {
											title: data.message,
											text:  data.description
										});

								anchor.parents('li').slideUp();
							}
						}
					});
				});
			},

			allViewUnloaded: function(subView) {

			}
		}
	}
})

/*
 *	find contact view module
 *  -----------------------------------------------------------------------------------------------------------
 */ 

.module('find_view', function(context) {
	var query = $ = context.query,
		request	= context.request,
		require = context.require,
		notify = require('notify'),
		dialog = require('actionDialog');

	
	return {
		events: {

			findViewLoaded: function(subView) {
				console.log('find loaded');

				dialog.create('.add_to_contacts', function(button) {
					request({
						url: button.attr('href'),
						type: 'post',
						dataType: 'json',
						success: function(data) {

							if(data.success == true){
								notify.create('create', {
											title: data.message,
											text:  data.description
										});

								button.parents('li').slideUp();
							}
						}
					});
				});			
			},

			findViewUnloaded: function(subView) {

			}
		}
	}
})

/*
 *	pending contact view module
 *  -----------------------------------------------------------------------------------------------------------
 */ 

.module('pending_view', function(context) {
	var query = $ = context.query,
		request	= context.request,
		require = context.require,
		notify = require('notify'),
		processing = false;

	
	return {
		events: {

			pending_requestsViewLoaded: function(subView) {
				console.log('pending loaded');

				var buttons = query('.pending_request');

				buttons.on('click', function(e){
					if(!processing){
						processing = true;
						var anchor = $(this);
						request({
							url: anchor.attr('href'),
							type: 'get',
							dataType: 'json',
							success: function(data) {

								if(data.success == true){
									notify.create('create', {
												title: data.message,
												text:  data.description
											});
								anchor.parents('li').slideUp();

								}
							},
							complete: function() {
								processing = false;
							}

						});
					}					
					e.preventDefault();
				}) 
			},

			pending_requestsViewUnloaded: function(subView) {

			}
		}
	}
})